<%@ page import="com.wuming.caiwu.security.SecurityHander" %><%--
  Created by IntelliJ IDEA.
  User: hanfei
  Date: 18/6/26
  Time: 下午5:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>订单信息管理</title>
    <link rel="stylesheet" href="${allServerPath}static/layui/layui/css/layui.css">
    <link rel="stylesheet" href="${allServerPath}static/layui/css/formSelects-v4.css">
    <style>
        .query-condition {
            margin-top: 15px;
        }
    </style>
</head>
<body style="background-color: #f0f2f5;">
<div style="padding: 15px;margin: 15px;background-color: #fff;">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>订单信息管理</legend>
    </fieldset>
    <form id="queryForm" class="layui-form" action="">
        <div class="" style="margin-top: 15px;">
            <div class="layui-row query-condition">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">订单编号</label>

                        <div class="layui-input-inline">
                            <input type="text" id="orderSn" name="orderSn" placeholder="请输入订单编号"
                                   lay-verify="orderSn"
                                   autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">订单状态</label>

                        <div class="layui-input-inline">
                            <select id="status" name="status"
                                    lay-filter="status">
                                <option value=""></option>
                                <option value="0">未支付</option>
                                <option value="1">支付中</option>
                                <option value="2">已支付</option>
                                <option value="3">已收货</option>
                                <option value="4">已完成</option>
                            </select>
                        </div>

                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">会员昵称</label>

                        <div class="layui-input-inline">
                            <input type="text" id="memberUsername" name="memberUsername" placeholder="请输入会员昵称"
                                   lay-verify="memberUsername"
                                   autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">门店名</label>

                        <div class="layui-input-inline">
                            <input type="text" id="receiverName" name="receiverName" placeholder="请输入门店名"
                                   lay-verify="receiverName"
                                   autocomplete="off"
                                   class="layui-input">
                        </div>

                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">创建时间</label>

                        <div class="layui-input-inline">
                            <input type="text" name="startDate" id="startDate" placeholder=""
                                   autocomplete="off"
                                   class="layui-input">
                        </div>
                        <div class="layui-input-inline">
                            <input type="text" name="endDate" id="endDate" placeholder=""
                                   autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <div class="layui-row query-condition" style="padding: 9px 15px;margin-bottom: 20px;">
        <button name="btnQuery" class="layui-btn layui-btn-normal" data-type="reload">查询搜索</button>
        <button id="btnExport" name="btnExport" class="layui-btn layui-btn-normal" data-type="export">导出</button>
    </div>

    <table class="layui-hide" id="studentsInfo" lay-filter="demo" lay-data="{height: 'full-200'}">
    </table>
</div>


<script type="text/html" id="barDemo">
    <shiro:hasAnyRoles
            name="super:manage,area:manage">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs" lay-event="del">删除</a>
    </shiro:hasAnyRoles>
    <shiro:hasAnyRoles
        name="factory:manage">
    {{#  if(d.status == '4'){ }}
    <a class="layui-btn layui-btn-xs"  lay-event="applyVerify">申请结算</a>
    {{#  }  }}
    </shiro:hasAnyRoles>
    <shiro:hasAnyRoles
        name="factory:manage">
    {{#  if(d.status == '1'){ }}
    <a class="layui-btn layui-btn-xs"  lay-event="sendGoods">发货</a>
    {{#  }  }}
    </shiro:hasAnyRoles>
    <shiro:hasAnyRoles
        name="mendian:manage">
    {{#  if(d.status == '2'){ }}
    <a class="layui-btn layui-btn-xs"  lay-event="confirmGoods">确认收货</a>
    {{#  }  }}
    </shiro:hasAnyRoles>
    <shiro:hasAnyRoles
            name="factory:manage">
        {{#  if(d.status == '3'){ }}
        <a class="layui-btn layui-btn-xs"  lay-event="completeOrder">完成订单</a>
        {{#  }  }}
    </shiro:hasAnyRoles>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail">查看详情</a>
</script>

<script type="text/html" id="paypriceTpl">
    <span style="color: #F581B1;">{{d.payAmount}}</span>
</script>


<script type="text/html" id="statusTpl">
    {{#  if(d.status == '0' || d.status == '12'){ }}
    <span>未支付</span>
    {{#  } else if(d.status == '1'){ }}
    <span>已付款</span>
    {{#  } else if(d.status == '2'){ }}
    <span>已发货</span>
    {{#  } else if(d.status == '3'){ }}
    <span>已收货</span>
    {{#  } else if(d.status == '4'){ }}
    <span>已完成</span>
    {{#  } }}
</script>

<script src="${allServerPath}static/layui/layui/layui.js"></script>
<script>
    layui.config({
        base: '${allServerPath}static/layui/build/js/'
    }).extend({
        formSelects: 'formSelects-v4.min'
    }).use(['table', 'jquery', 'navbar', 'laydate', 'form', 'element', 'formSelects'], function () {
        var table = layui.table,
            $ = layui.jquery,
            form = layui.form,
            laydate = layui.laydate,
            formSelects = layui.formSelects,
            element = layui.element;

        $("#btnExport").click(function () {
            var type = '${type}';
            var whereParam = {};
            if(type == "0"){
                whereParam['supplyId'] = '<%=SecurityHander.getCurrentUserId()%>';
                whereParam['orderType'] = 2;
            }else if(type == "1"){
                whereParam['storeId'] = '<%=SecurityHander.getCurrentUserId()%>';
                whereParam['orderType'] = 2;
            }
            var memberUsername = $('#memberUsername').val();
            whereParam['memberUsername'] = memberUsername;
            var status = $("#status").val();
            whereParam['status'] = status;
            var orderSn = $("#orderSn").val();
            whereParam['orderSn'] = orderSn;
            var startDate = $("#startDate").val();
            whereParam['startDate'] = startDate;
            var endDate = $("#endDate").val();
            whereParam['endDate'] = endDate;
            var receiverName = $("#receiverName").val();
            whereParam['receiverName'] = receiverName;
            $.ajax({
                    url: '${allServerPath}/web/order/exportOrder.htmls',
                    method: 'post',
                    data: whereParam,
                    dataType: 'json',
                    success: function (result) {
                        if (result.success) {
                            layer.msg('导出成功.');
                            PdfUtil.pdfFile("${allServerPath}web/base/downLoadTempFile.htmls", result.data);
                        }
                    },
                    error: function () {
                        layer.msg('导出失败,请稍后重试.');
                    }
                }
            );
        });

        laydate.render({
            elem: '#startDate',
            type: 'datetime'
        });

        laydate.render({
            elem: '#endDate',
            type: 'datetime'
        });

        var type = '${type}';
        var whereParam = {};
        if(type == "0"){
            whereParam['supplyId'] = '<%=SecurityHander.getCurrentUserId()%>';
            whereParam['orderType'] = 2;
        }else if(type == "1"){
            whereParam['storeId'] = '<%=SecurityHander.getCurrentUserId()%>';
            whereParam['orderType'] = 2;
        }

        table.render({ //其它参数在此省略
            elem: '#studentsInfo'
//            , height: 500
            , url: '${allServerPath}web/order/list.htmls?t=' + Date.parse(new Date())
            , cols: [[ //表头
                {field: 'id', width: 80, title: '序号', fixed: 'left'}
                , {field: 'memberUsername', title: '会员名', align: 'center', width: 200}
                , {field: 'totalAmount', title: '市场价', align: 'center', width: 120}
                , {field: 'promotionAmount', title: '优惠金额', align: 'center', width: 120}
                , {field: 'payAmount', title: '订单金额', align: 'center', width: 120, templet: '#paypriceTpl'}
                , {field: 'status', title: '订单状态', align: 'center', width: 120, templet: '#statusTpl'}
                , {field: 'receiverName', title: '门店名', align: 'center', width: 120}
                , {field: 'receiverPhone', title: '门店电话', align: 'center', width: 120}
                , {field: 'receiverDetailAddress', title: '门店地址', align: 'center', width: 300}
                , {field: 'createTime', title: '创建时间', align: 'center', width: 260}
                , {fixed: 'right', title: '操作', align: 'center', templet: '#barDemo'}
            ]],
            id: 'studentContent',
            where: whereParam, //如果无需传递额外参数，可不加该参数
            method: 'get', //如果无需自定义HTTP类型，可不加该参数
            page: true,
            limits: [10, 20, 30, 50, 100, 200],
            limit: 10,
            request: {
                pageName: 'pageIndex', //页码的参数名称，默认：page
                limitName: 'pageSize'
            }, //如果无需自定义请求参数，可不加该参数
            response: {
                statusName: 'code' //数据状态的字段名称，默认：code
                , statusCode: 200 //成功的状态码，默认：0
                , msgName: 'message' //状态信息的字段名称，默认：msg
                , countName: 'total' //数据总数的字段名称，默认：count
                , dataName: 'data' //数据列表的字段名称，默认：data
            }, //如果无需自定义数据响应名称，可不加该参数
            done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                console.log(res);

                //得到当前页码
                console.log(curr);

                //得到数据总量
                console.log(count);
            },
            text: {
                none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
        });


        table.on('tool(demo)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                //layer.msg('ID：' + data.id + ' 的查看操作')
                parent.window.tab.tabAdd({
                    'id': 'edit_' + data.id,
                    'title': '编辑订单信息',
                    'icon': '',
                    'url': '${allServerPath}web/order/editOrderDetail/' + data.id + '.htmls'
                });
            } else if (obj.event === 'detail') {
                //layer.msg('ID：' + data.id + ' 的查看操作')
                parent.window.tab.tabAdd({
                    'id': 'detail_' + data.id,
                    'title': '查看订单信息',
                    'icon': '',
                    'url': '${allServerPath}web/order/editOrder/' + data.id + '.htmls?type=0'
                });
            } else if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                            url: '${allServerPath}web/order/delete/' + data.id + ".htmls",
                            method: 'get',
                            dataType: 'json',
                            success: function (result) {
                                if (result.success) {
                                    obj.del();
                                    layer.close(index);
                                    table.reload('studentContent', {});
                                }
                            },
                            error: function () {
                                layer.msg('删除失败,请稍后重试.');
                            }
                        }
                    );
                });
            } else if (obj.event === 'sendGoods') {
                layer.confirm('请确定商品已经发货', function (index) {
                    $.ajax({
                            url: '${allServerPath}web/order/sendGoods/' + data.id + ".htmls",
                            method: 'get',
                            dataType: 'json',
                            success: function (result) {
                                if (result.success) {
                                    layer.msg("商品发货成功.");
                                    table.reload('studentContent', {});
                                }
                            },
                            error: function () {
                                layer.msg('发货失败,请稍后重试.');
                            }
                        }
                    );
                });
            } else if(obj.event === "confirmGoods"){
                layer.confirm('请确定已经收到货？', function (index) {
                    $.ajax({
                            url: '${allServerPath}web/order/confirmGoods/' + data.id + ".htmls",
                            method: 'get',
                            dataType: 'json',
                            success: function (result) {
                                if (result.success) {
                                    layer.msg("商品收货成功.");
                                    table.reload('studentContent', {});
                                }
                            },
                            error: function () {
                                layer.msg('收货失败,请稍后重试.');
                            }
                        }
                    );
                });
            } else if(obj.event === "completeOrder"){
                layer.confirm('请确定订单已经完成？', function (index) {
                    $.ajax({
                            url: '${allServerPath}web/order/completeOrder/' + data.id + ".htmls",
                            method: 'get',
                            dataType: 'json',
                            success: function (result) {
                                if (result.success) {
                                    layer.msg("订单结束成功.");
                                    table.reload('studentContent', {});
                                }
                            },
                            error: function () {
                                layer.msg('订单结束失败,请稍后重试.');
                            }
                        }
                    );
                });
            }
        });

        $('button[name="btnQuery"]').on('click', function () {
            var type = '${type}';
            var whereParam = {};
            if(type == "0"){
                whereParam['supplyId'] = '<%=SecurityHander.getCurrentUserId()%>';
                whereParam['orderType'] = 2;
            }else if(type == "1"){
                whereParam['storeId'] = '<%=SecurityHander.getCurrentUserId()%>';
                whereParam['orderType'] = 2;
            }
            var memberUsername = $('#memberUsername').val();
            whereParam['memberUsername'] = memberUsername;
            var status = $("#status").val();
            whereParam['status'] = status;
            var orderSn = $("#orderSn").val();
            whereParam['orderSn'] = orderSn;
            var startDate = $("#startDate").val();
            whereParam['startDate'] = startDate;
            var endDate = $("#endDate").val();
            whereParam['endDate'] = endDate;
            var receiverName = $("#receiverName").val();
            whereParam['receiverName'] = receiverName;

            table.reload('studentContent', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: whereParam
            });
        })


        /**
         * 下载文件链接
         */
        var PdfUtil = {
            pdfFile: function (url, data) {
                if (url && data) {
                    var inputs = '';
                    for (var p in data) {
                        inputs += '<input type="hidden" name="' + p + '" value="' + data[p]
                            + '" />';
                    }
                    $('<form action="' + url + '" method="post">' + inputs + '</form>')
                        .appendTo('body').submit().remove();
                }
            }

        };
    })

</script>
</body>
</html>
